//
//  SISSObtainEvidenceShowViewController.m
//  SISSAPP
//
//  Created by 袁应荣 on 2018/10/31.
//  Copyright © 2018年 优房网. All rights reserved.
//

#import "SISSObtainEvidenceShowViewController.h"
#import "SISSObtainEvidenceShowViewModel.h"
#import "SISSObtainEvidenceEnrollViewController.h"
#import "SISSHouseProgressViewController.h"
#import "SISSNetworkingManager+HouseProgress.h"

@interface SISSObtainEvidenceShowViewController ()

/* 录入进度按钮 */
@property (weak, nonatomic) UIButton *editTypeInButton;

@property (strong, nonatomic) SISSObtainEvidenceShowViewModel *obtainEvidenceShowViewModel;

@property (strong, nonatomic) SISSObtainEvidenceEnrollModel *mortgageInfoModel;

@end

@implementation SISSObtainEvidenceShowViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"取旧证，注销抵押";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(backItemAction)];
    [self requestData];
}

/**
 返回按钮点击事件
 */
- (void)backItemAction {
    
    for (UIViewController *controller in self.navigationController.viewControllers) {
        if ([controller isKindOfClass:[SISSHouseProgressViewController class]]) {
            [self.navigationController popToViewController:controller animated:YES];
        }
    }
}

- (void)requestData {
    [kNetworkManager requestMortgageInfoWithAhbpId:self.ahbpId success:^(SISSObtainEvidenceEnrollModel *mortgageInfoModel) {
        self.obtainEvidenceShowViewModel.dataArr[0][1].cellType = 1;
        [self.obtainEvidenceShowViewModel assignmentWithObtainEvidenceEnrollModel:mortgageInfoModel];
        self.mortgageInfoModel = mortgageInfoModel;
        if (self.isHistory && !mortgageInfoModel) {
            for (UIView *view in self.view.subviews) {
                view.hidden = YES;
            }
        }
    } failed:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}

#pragma mark - 代理
#pragma mark -- UITableViewDelegate & UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [self.obtainEvidenceShowViewModel getSectionCount];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.obtainEvidenceShowViewModel getRowsInSection:section];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [self.obtainEvidenceShowViewModel getCell:tableView indexPath:indexPath];
    cell.userInteractionEnabled = NO;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}



- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return [self.obtainEvidenceShowViewModel getHeightForHeaderInSection:section];
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    return [self.obtainEvidenceShowViewModel getViewHeaderInSection:section];
}

// 取消tableView组头悬停
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat sectionHeaderHeight = 40;
    if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
    } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }
}


#pragma mark - 按钮点击事件
#pragma mark -- 确定按钮点击事件
- (void)editTypeInButtonClickAction {
    
    SISSObtainEvidenceEnrollViewController *vc = [[SISSObtainEvidenceEnrollViewController alloc] init];
    vc.ahbpId = self.ahbpId;
    vc.dataSource = self.obtainEvidenceShowViewModel.dataArr;
    vc.mortgageInfoModel = self.mortgageInfoModel;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)setDataSource:(NSArray *)dataSource {
    [super setDataSource:dataSource];
    self.obtainEvidenceShowViewModel.dataArr = dataSource.copy;
    self.obtainEvidenceShowViewModel.dataArr[0][1].cellType = 1;
    self.obtainEvidenceShowViewModel.dataArr[1][0].cellType = 1;
    [self.tableView reloadData];
}

#pragma mark - UI
- (void)setupSubviews {
    [super setupSubviews];
    
    SISSBaseTableView *tableView = [[SISSBaseTableView alloc] init];
    tableView.estimatedRowHeight = 44;
    tableView.rowHeight = UITableViewAutomaticDimension;
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.obtainEvidenceShowViewModel registContractClass:tableView];
    [self.view addSubview:tableView];
    self.tableView = tableView;
    self.obtainEvidenceShowViewModel.tableView = tableView;
    
    UIView *footerView = [[UIView alloc ] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 88)];
    
    UIButton *editTypeInButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [editTypeInButton setTitle:@"编辑进度" forState:UIControlStateNormal];
    [editTypeInButton setBackgroundColor:MAIN_COLOR];
    [editTypeInButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [editTypeInButton addTarget:self action:@selector(editTypeInButtonClickAction) forControlEvents:UIControlEventTouchUpInside];
    editTypeInButton.layer.cornerRadius = 4;
    editTypeInButton.layer.masksToBounds = YES;
    editTypeInButton.hidden = self.isHistory;
    [self.view addSubview:editTypeInButton];
    self.editTypeInButton = editTypeInButton;
    
    [self.editTypeInButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.mas_bottomLayoutGuideTop);
        //        make.width.mas_equalTo(SCREEN_WIDTH);
        make.height.mas_equalTo(44);
    }];
    
    self.tableView.tableFooterView = footerView;
}

- (SISSObtainEvidenceShowViewModel *)obtainEvidenceShowViewModel {
    if (!_obtainEvidenceShowViewModel) {
        _obtainEvidenceShowViewModel = [[SISSObtainEvidenceShowViewModel alloc] init];
    }
    return _obtainEvidenceShowViewModel;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
